<html>
<head><meta charset="utf-8"><title>Ambiguity in diagnostics (#72690) · t-compiler/wg-diagnostics · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/index.html">t-compiler/wg-diagnostics</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity.20in.20diagnostics.20(.2372690).html">Ambiguity in diagnostics (#72690)</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="199848046"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity%20in%20diagnostics%20%28%2372690%29/near/199848046" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nathan Corbyn <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity.20in.20diagnostics.20(.2372690).html#199848046">(Jun 05 2020 at 08:34)</a>:</h4>
<p>I've put a lot of effort into understanding <a href="https://github.com/rust-lang/rust/issues/72690">#72690</a> and I think I have a pretty good grasp of what's going on but I'm really struggling to come up with any kind of sensible solution - I was wondering if I could get some help/mentoring/opinions</p>



<a name="199848059"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity%20in%20diagnostics%20%28%2372690%29/near/199848059" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nathan Corbyn <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity.20in.20diagnostics.20(.2372690).html#199848059">(Jun 05 2020 at 08:34)</a>:</h4>
<p>(I'll put a summary of what I understand about the issue in this thread)</p>



<a name="199849021"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity%20in%20diagnostics%20%28%2372690%29/near/199849021" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nathan Corbyn <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity.20in.20diagnostics.20(.2372690).html#199849021">(Jun 05 2020 at 08:45)</a>:</h4>
<p>Setting the scene:</p>
<ul>
<li>We've got a type inference error and we know we need more information to resolve it</li>
<li>We want to suggest useful things like adding type bounds to closures arguments/patterns etc</li>
<li>We're only given a <code>BodyId</code> and the inference target and somehow we need to find the expression in the body that actually caused the inference error</li>
</ul>
<p>The issue:</p>
<ul>
<li>At the moment, we use a visitor (<code>FindHirNodeVisitor</code>) to search the body for hir nodes that are candidates for suggesting on</li>
<li>
<p>It's a very simple tree walk and the candidate selection policy is essentially</p>
<ol>
<li>'is the type of this node the target type?'</li>
<li>'does the type of this node contain the target type?'</li>
<li>'does the type of this node contain an inference variable that can be unified with the target type?'</li>
</ol>
</li>
<li>
<p>Any time we find two candidates of the same variety (i.e., two closures) we always choose the latter</p>
</li>
<li>The ultimate problem is that when the inference target was something like <code>String</code> or <code>u32</code>, the candidate selection policy is far too liberal and always choosing later nodes results in us reporting errors at the end of functions caused by inference errors at the start</li>
<li>A concrete example is something like:</li>
</ul>
<div class="codehilite"><pre><span></span><code><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nb">String</span>::<span class="n">from</span><span class="p">(</span><span class="s">&quot;x&quot;</span><span class="p">.</span><span class="n">as_ref</span><span class="p">());</span><span class="w"> </span><span class="c1">// this will cause an error</span>
<span class="w">    </span><span class="c1">// but you can put as much code as you like here</span>
<span class="w">    </span><span class="o">|</span><span class="n">x</span>: <span class="nb">String</span><span class="o">|</span><span class="w"> </span><span class="n">x</span><span class="p">;</span><span class="w"> </span><span class="c1">// and the error will still be reported here and ask for `x` to be given a type bound</span>
<span class="w">    </span><span class="c1">// this is clearly ridiculous as the last line is absolutely fine and `x` already has a bound</span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>


<p>(<a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=b32b14cf76ec39003cf20bf2b13f0455">playground</a>)</p>



<a name="199849269"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity%20in%20diagnostics%20%28%2372690%29/near/199849269" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nathan Corbyn <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity.20in.20diagnostics.20(.2372690).html#199849269">(Jun 05 2020 at 08:48)</a>:</h4>
<p>This is an extremely important diagnostic, but as things stand, the error messages are completely meaningless in situations like the example I've given. I'm nervous that there's just too much ambiguity in the current approach to recovering hir nodes from bodies given so little information to do this correctly...</p>



<a name="199849538"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity%20in%20diagnostics%20%28%2372690%29/near/199849538" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nathan Corbyn <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity.20in.20diagnostics.20(.2372690).html#199849538">(Jun 05 2020 at 08:51)</a>:</h4>
<p>So - I have two questions really:</p>
<ol>
<li>How much effort would it be to refactor <code>ObligationCause</code> to give higher granularity in function bodies?</li>
<li>Are there any other obvious approaches? Should we be looking to make the <code>needs_type_info_err</code> significantly more conservative and leave things as they are?</li>
</ol>



<a name="199851786"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity%20in%20diagnostics%20%28%2372690%29/near/199851786" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity.20in.20diagnostics.20(.2372690).html#199851786">(Jun 05 2020 at 09:14)</a>:</h4>
<p>By how much do the error message deteriorate if we replace </p>
<div class="codehilite"><pre><span></span><code><span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="n">err_span</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="n">pattern</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">local_visitor</span><span class="p">.</span><span class="n">found_arg_pattern</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="n">pattern</span><span class="p">.</span><span class="n">span</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
</code></pre></div>


<p>with</p>
<div class="codehilite"><pre><span></span><code><span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="n">err_span</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="n">pattern</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">local_visitor</span><span class="w"></span>
<span class="w">                     </span><span class="p">.</span><span class="n">found_arg_pattern</span><span class="p">.</span><span class="n">filter</span><span class="p">(</span><span class="o">|</span><span class="n">arg_pat</span><span class="o">|</span><span class="w"> </span><span class="n">span</span><span class="p">.</span><span class="n">contains</span><span class="p">(</span><span class="n">arg_pat</span><span class="p">.</span><span class="n">span</span><span class="p">))</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="n">pattern</span><span class="p">.</span><span class="n">span</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="199851887"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity%20in%20diagnostics%20%28%2372690%29/near/199851887" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nathan Corbyn <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity.20in.20diagnostics.20(.2372690).html#199851887">(Jun 05 2020 at 09:14)</a>:</h4>
<p><code>span</code> there is the body's span, so not at all</p>



<a name="199851932"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity%20in%20diagnostics%20%28%2372690%29/near/199851932" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nathan Corbyn <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity.20in.20diagnostics.20(.2372690).html#199851932">(Jun 05 2020 at 09:15)</a>:</h4>
<p>:((</p>



<a name="199852015"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity%20in%20diagnostics%20%28%2372690%29/near/199852015" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity.20in.20diagnostics.20(.2372690).html#199852015">(Jun 05 2020 at 09:16)</a>:</h4>
<p>In <code>maybe_report_ambiguity</code> it's the obligation span, which should not be the body's span? <span aria-label="thinking" class="emoji emoji-1f914" role="img" title="thinking">:thinking:</span></p>



<a name="199852074"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity%20in%20diagnostics%20%28%2372690%29/near/199852074" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nathan Corbyn <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity.20in.20diagnostics.20(.2372690).html#199852074">(Jun 05 2020 at 09:16)</a>:</h4>
<p>Hmmm okay - I thought I tried this yesterday but I'll give it another go</p>



<a name="199852255"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity%20in%20diagnostics%20%28%2372690%29/near/199852255" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nathan Corbyn <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity.20in.20diagnostics.20(.2372690).html#199852255">(Jun 05 2020 at 09:18)</a>:</h4>
<p>Ah! Okay that did make a difference - it's still not correct but that's really helpful thanks a lot</p>



<a name="199852283"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity%20in%20diagnostics%20%28%2372690%29/near/199852283" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nathan Corbyn <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity.20in.20diagnostics.20(.2372690).html#199852283">(Jun 05 2020 at 09:19)</a>:</h4>
<p>I think moving that filter into the visitor will probably fix a lot of things</p>



<a name="199852303"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity%20in%20diagnostics%20%28%2372690%29/near/199852303" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity.20in.20diagnostics.20(.2372690).html#199852303">(Jun 05 2020 at 09:19)</a>:</h4>
<p>What is the current output?</p>



<a name="199852325"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity%20in%20diagnostics%20%28%2372690%29/near/199852325" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nathan Corbyn <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity.20in.20diagnostics.20(.2372690).html#199852325">(Jun 05 2020 at 09:19)</a>:</h4>
<div class="codehilite"><pre><span></span><code>error[E0283]: type annotations needed for `std::string::String`
  --&gt; src/test/ui/issues/issue-72690.rs:10:5
   |
10 |     String::from(&quot;x&quot;.as_ref()); //~ ERROR type annotations needed
   |     ^^^^^^^^^^^^
11 |     |x: String| x;
   |      - consider giving this closure parameter a type
   |
   = note: cannot satisfy `std::string::String: std::convert::From&lt;&amp;_&gt;`
   = note: required by `std::convert::From::from`
</code></pre></div>



<a name="199852433"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity%20in%20diagnostics%20%28%2372690%29/near/199852433" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity.20in.20diagnostics.20(.2372690).html#199852433">(Jun 05 2020 at 09:20)</a>:</h4>
<p>jup, probably also in line 433 <span aria-label="thumbs up" class="emoji emoji-1f44d" role="img" title="thumbs up">:thumbs_up:</span> Or you check it in the <code>FoundHirNodeVisitor</code>.</p>



<a name="199852448"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity%20in%20diagnostics%20%28%2372690%29/near/199852448" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity.20in.20diagnostics.20(.2372690).html#199852448">(Jun 05 2020 at 09:20)</a>:</h4>
<p>Thanks for looking into this <span aria-label="heart" class="emoji emoji-2764" role="img" title="heart">:heart:</span></p>



<a name="199852472"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity%20in%20diagnostics%20%28%2372690%29/near/199852472" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nathan Corbyn <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity.20in.20diagnostics.20(.2372690).html#199852472">(Jun 05 2020 at 09:21)</a>:</h4>
<p>Thanks so much!</p>



<a name="199853099"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity%20in%20diagnostics%20%28%2372690%29/near/199853099" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nathan Corbyn <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity.20in.20diagnostics.20(.2372690).html#199853099">(Jun 05 2020 at 09:28)</a>:</h4>
<p>I'm wondering if we would lose the ability to report on things like this by adding that check?</p>
<div class="codehilite"><pre><span></span><code><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="o">|</span><span class="n">x</span><span class="o">|</span><span class="w"> </span><span class="nb">String</span>::<span class="n">from</span><span class="p">(</span><span class="n">x</span><span class="p">.</span><span class="n">as_ref</span><span class="p">());</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="199853187"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity%20in%20diagnostics%20%28%2372690%29/near/199853187" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nathan Corbyn <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity.20in.20diagnostics.20(.2372690).html#199853187">(Jun 05 2020 at 09:29)</a>:</h4>
<p>(It seems fine! <span aria-label="tada" class="emoji emoji-1f389" role="img" title="tada">:tada:</span>)</p>



<a name="199855242"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity%20in%20diagnostics%20%28%2372690%29/near/199855242" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nathan Corbyn <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity.20in.20diagnostics.20(.2372690).html#199855242">(Jun 05 2020 at 09:52)</a>:</h4>
<p>I added a similar check for local patterns so we don't get tripped up by things like</p>
<div class="codehilite"><pre><span></span><code><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nb">String</span>::<span class="n">from</span><span class="p">(</span><span class="s">&quot;x&quot;</span><span class="p">.</span><span class="n">as_ref</span><span class="p">());</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">_</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">String</span>::<span class="n">from</span><span class="p">(</span><span class="s">&quot;x&quot;</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>


<p>but a whole bunch of UI tests start failing because local patterns aren't contained within the obligation spans so we end up with notes missing</p>



<a name="199855303"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity%20in%20diagnostics%20%28%2372690%29/near/199855303" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity.20in.20diagnostics.20(.2372690).html#199855303">(Jun 05 2020 at 09:53)</a>:</h4>
<p>can you send an example of what's failing now?</p>



<a name="199855317"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity%20in%20diagnostics%20%28%2372690%29/near/199855317" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nathan Corbyn <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity.20in.20diagnostics.20(.2372690).html#199855317">(Jun 05 2020 at 09:53)</a>:</h4>
<p>Is there a way to get the span of the expression that the pattern is bound over?</p>



<a name="199855396"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity%20in%20diagnostics%20%28%2372690%29/near/199855396" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nathan Corbyn <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity.20in.20diagnostics.20(.2372690).html#199855396">(Jun 05 2020 at 09:54)</a>:</h4>
<p>A typical stderr diff is something like</p>
<div class="codehilite"><pre><span></span><code>-   error[E0282]: type annotations needed for `std::vec::Vec&lt;T&gt;`
+   error[E0282]: type annotations needed
2     --&gt; $DIR/vector-no-ann.rs:2:16
3      |
4   LL |     let _foo = Vec::new();

-      |         ----   ^^^^^^^^ cannot infer type for type parameter `T`
-      |         |
-      |         consider giving `_foo` the explicit type `std::vec::Vec&lt;T&gt;`, where the type parameter `T` is specified
+      |                ^^^^^^^^ cannot infer type for type parameter `T`
8
9   error: aborting due to previous error
10
</code></pre></div>



<a name="199855455"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity%20in%20diagnostics%20%28%2372690%29/near/199855455" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nathan Corbyn <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity.20in.20diagnostics.20(.2372690).html#199855455">(Jun 05 2020 at 09:54)</a>:</h4>
<p>The obligation comes from the expression the pattern binds, but the pattern span itself is outside of that</p>



<a name="199855662"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity%20in%20diagnostics%20%28%2372690%29/near/199855662" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nathan Corbyn <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity.20in.20diagnostics.20(.2372690).html#199855662">(Jun 05 2020 at 09:56)</a>:</h4>
<p>Got it - it's <code>local.span</code></p>



<a name="199857117"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity%20in%20diagnostics%20%28%2372690%29/near/199857117" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nathan Corbyn <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity.20in.20diagnostics.20(.2372690).html#199857117">(Jun 05 2020 at 10:13)</a>:</h4>
<p>The last failures I'm getting are now this shape:</p>
<div class="codehilite"><pre><span></span><code>-   error[E0282]: type annotations needed for `std::option::Option&lt;T&gt;`
+   error[E0282]: type annotations needed
2     --&gt; $DIR/unboxed-closures-failed-recursive-fn-2.rs:16:32
3      |
-   LL |     let mut closure0 = None;
-      |         ------------ consider giving `closure0` the explicit type `std::option::Option&lt;T&gt;`, with the type parameters specified
-   ...
+   LL |                 match closure0.take() {
+      |                       --------------- this method call resolves to `std::option::Option&lt;T&gt;`
+   LL |                     Some(c) =&gt; {
7   LL |                         return c();
8      |                                ^^^ cannot infer type
9      |
</code></pre></div>



<a name="199857225"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity%20in%20diagnostics%20%28%2372690%29/near/199857225" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nathan Corbyn <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity.20in.20diagnostics.20(.2372690).html#199857225">(Jun 05 2020 at 10:15)</a>:</h4>
<p>Where we could previously relate inference errors with locals across the function body, we no longer can - I'm wondering if this is a fair trade for avoiding incorrect suggestions? Or if I should leave the behaviour regarding locals as is?</p>



<a name="199858077"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity%20in%20diagnostics%20%28%2372690%29/near/199858077" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity.20in.20diagnostics.20(.2372690).html#199858077">(Jun 05 2020 at 10:26)</a>:</h4>
<p>I think this change is worth it. Incorrect error messages tend can be quite off-putting, especially for newcomers.</p>



<a name="199858149"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity%20in%20diagnostics%20%28%2372690%29/near/199858149" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nathan Corbyn <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity.20in.20diagnostics.20(.2372690).html#199858149">(Jun 05 2020 at 10:27)</a>:</h4>
<p>With local patterns - even if we incorrectly identify one, the actual source of the inference error is still highlighted properly</p>



<a name="199858169"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity%20in%20diagnostics%20%28%2372690%29/near/199858169" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nathan Corbyn <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity.20in.20diagnostics.20(.2372690).html#199858169">(Jun 05 2020 at 10:27)</a>:</h4>
<p>So it's less of an issue than arg patterns</p>



<a name="199858214"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity%20in%20diagnostics%20%28%2372690%29/near/199858214" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nathan Corbyn <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity.20in.20diagnostics.20(.2372690).html#199858214">(Jun 05 2020 at 10:28)</a>:</h4>
<div class="codehilite"><pre><span></span><code><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">vec</span><span class="o">!</span><span class="p">[];</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="199858254"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity%20in%20diagnostics%20%28%2372690%29/near/199858254" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nathan Corbyn <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity.20in.20diagnostics.20(.2372690).html#199858254">(Jun 05 2020 at 10:28)</a>:</h4>
<p>Also how many new-comers have written this - we want to be able to suggest adding annotations to <code>x</code>, but the target span will be in the expansion of <code>vec!</code> so we can't just check containment</p>



<a name="199858422"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity%20in%20diagnostics%20%28%2372690%29/near/199858422" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity.20in.20diagnostics.20(.2372690).html#199858422">(Jun 05 2020 at 10:30)</a>:</h4>
<p>That's true...</p>



<a name="199858465"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity%20in%20diagnostics%20%28%2372690%29/near/199858465" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity.20in.20diagnostics.20(.2372690).html#199858465">(Jun 05 2020 at 10:30)</a>:</h4>
<p>A potential fix might be to only consider local patterns if we find exactly one.</p>



<a name="199858478"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity%20in%20diagnostics%20%28%2372690%29/near/199858478" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nathan Corbyn <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity.20in.20diagnostics.20(.2372690).html#199858478">(Jun 05 2020 at 10:30)</a>:</h4>
<p>I think I'm gonna open a PR with a big test case and the fix for arg patterns but leave local patterns and include a comment about the trade-off</p>



<a name="199858555"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity%20in%20diagnostics%20%28%2372690%29/near/199858555" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nathan Corbyn <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity.20in.20diagnostics.20(.2372690).html#199858555">(Jun 05 2020 at 10:31)</a>:</h4>
<p>That's a good point - I'll see if that hurts any test cases</p>



<a name="199858657"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity%20in%20diagnostics%20%28%2372690%29/near/199858657" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity.20in.20diagnostics.20(.2372690).html#199858657">(Jun 05 2020 at 10:32)</a>:</h4>
<blockquote>
<p>A potential fix might be to only consider local patterns if we find exactly one.</p>
</blockquote>
<p>hm, thinking a bit more about this, I think it still won't fix </p>
<div class="codehilite"><pre><span></span><code><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nb">String</span>::<span class="n">from</span><span class="p">(</span><span class="s">&quot;x&quot;</span><span class="p">.</span><span class="n">as_ref</span><span class="p">());</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">_</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">String</span>::<span class="n">from</span><span class="p">(</span><span class="s">&quot;x&quot;</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>


<p>which has exactly one (incorrect) local pattern</p>



<a name="199859132"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity%20in%20diagnostics%20%28%2372690%29/near/199859132" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nathan Corbyn <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity.20in.20diagnostics.20(.2372690).html#199859132">(Jun 05 2020 at 10:38)</a>:</h4>
<p>It does reduce the envelope of possible spurious suggestions though</p>



<a name="199862788"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity%20in%20diagnostics%20%28%2372690%29/near/199862788" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nathan Corbyn <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity.20in.20diagnostics.20(.2372690).html#199862788">(Jun 05 2020 at 11:22)</a>:</h4>
<p>In the end - really didn't justify the number of regressions for the reduction in the size of the envelope</p>



<a name="199862801"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity%20in%20diagnostics%20%28%2372690%29/near/199862801" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nathan Corbyn <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity.20in.20diagnostics.20(.2372690).html#199862801">(Jun 05 2020 at 11:22)</a>:</h4>
<p>Last regression is <code>src/ui/issues/issue-23046.rs</code></p>



<a name="199862832"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity%20in%20diagnostics%20%28%2372690%29/near/199862832" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nathan Corbyn <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity.20in.20diagnostics.20(.2372690).html#199862832">(Jun 05 2020 at 11:23)</a>:</h4>
<p>We used to get</p>
<div class="codehilite"><pre><span></span><code>error[E0282]: type annotations needed for `Expr&lt;&#39;_, VAR&gt;`
  --&gt; src/test/ui/issues/issue-23046.rs:17:15
   |
17 |     let ex = |x| {
   |               ^ consider giving this closure parameter the explicit type `Expr&lt;&#39;_, VAR&gt;`, where the type parameter `VAR` is specified

error: aborting due to previous error
</code></pre></div>


<p>but now it's</p>
<div class="codehilite"><pre><span></span><code>  --&gt; src/test/ui/issues/issue-23046.rs:18:9
   |
18 |         let_(add(x,x), |y| { //~ ERROR type annotations needed
   |         ^^^^ cannot infer type for type parameter `VAR` declared on the function `let_`
   |
help: give this closure an explicit return type without `_` placeholders
   |
19 |             let_(add(x, x), |x|-&gt; Expr&lt;&#39;_, _&gt; { x })})};
   |                                ^^^^^^^^^^^^^^^^   ^

error: aborting due to previous error
</code></pre></div>



<a name="199862998"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity%20in%20diagnostics%20%28%2372690%29/near/199862998" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nathan Corbyn <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity.20in.20diagnostics.20(.2372690).html#199862998">(Jun 05 2020 at 11:25)</a>:</h4>
<p>IIUC neither of these suggestions actually resolve the issue alone so it's probably ok??</p>



<a name="199863583"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity%20in%20diagnostics%20%28%2372690%29/near/199863583" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nathan Corbyn <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity.20in.20diagnostics.20(.2372690).html#199863583">(Jun 05 2020 at 11:32)</a>:</h4>
<p>Ok I've opened a PR - <a href="https://github.com/rust-lang/rust/issues/73027">#73027</a></p>



<a name="200447005"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity%20in%20diagnostics%20%28%2372690%29/near/200447005" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Esteban Küber <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Ambiguity.20in.20diagnostics.20(.2372690).html#200447005">(Jun 10 2020 at 17:17)</a>:</h4>
<blockquote>
<p>How much effort would it be to refactor ObligationCause to give higher granularity in function bodies?</p>
</blockquote>
<p>It would be a moderate amount of work, but it is probably the best course of action and the one that will be most likely to bear fruit</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>